<?php

require_once('ScoopitOAuth.php');
require_once('ScoopitAPI.php');


class ScoopitMetabox {

	private $scoopitOptions;
	private $db;
	private $userID;

	function __construct($db) {
		$this->scoopitOptions = get_option('share_on');
		$this->db = $db;
		
		
		add_action('wp_ajax_my_action', array($this,'my_action_callback'));
		add_action('load-post-new.php', array($this, 'scoopitMetaboxSetup'));
		add_action('publish_post', array($this, 'publish'));
	}
		
	function scoopitMetaboxSetup() {		
		add_action('add_meta_boxes', array($this, 'scoopitMetabox'));
		add_action('admin_footer', array($this, 'my_action_javascript'));
		$this->db->emptySharers();
	}
	
	function scoopitMetabox() {
		
		add_meta_box(
			'scoopit-post-class',					// Unique ID
			'ShareOn Scoop.it',						// Title
			array($this,'scoopitMetaboxContent'),	// Callback function
			'post',									// Admin page (or post type)
			'side',									// Context
			'high'									// Priority
		);		
	}
	
	function scoopitMetaboxContent() { 
		wp_nonce_field( plugin_basename( __FILE__ ), 'scoopitMetabox' );
		
		if (empty($this->scoopitOptions['scoopit_key']) || empty($this->scoopitOptions['scoopit_secret'])) :
			?><p>Please go to the ShareOn <a href="options-general.php?page=share_on.php">settings page</a></p><?php
		else :

			$scoopitOAuth = new ScoopitOAuth($this->db, $this->scoopitOptions);


			if (isset($_GET['oauth_verifier'])){
				$scoopitOAuth->startThirdLeg();
			}
				
			if ($scoopitOAuth->showLogin()) {
				$scoopitOAuth->login();
			} else {
				?><p>Select where to post on Scoop.It</p><?php
				$scoopitAPI = new ScoopitAPI($this->db, $this->scoopitOptions);
				$scoopitAPI->metaboxMainContent();
			}
		endif;
	}
	
	//ScoopitAPI->publish() relys on the fact that the topicID is added last to the array
	function my_action_javascript() {
	?>
		<script type="text/javascript">
	
		(function($) {
			var input = $("input:[type='checkbox'][name='sharers'],#select_topic");
			input.change( function()
			{
				var sharearray = new Array();
						
				input.each( function()
				{
					if ( this.checked ) {
						sharearray.push(this.value);
					}
					
				});
				
				$("#select_topic").each( function()
				{
					sharearray.push(this.value);
				});
				
				var data = {
						action: 'my_action',
						scoopitpost: sharearray
						};
				
				$.post(ajaxurl, data);
			});	
	
		})( jQuery );
		</script>
	<?php
	}

	public function my_action_callback() {	
		if (isset($_POST['scoopitpost']) || !in_array('-', $_POST['scoopitpost']))	
			$this->db->setSharers(serialize($_POST['scoopitpost']));
		else
			$this->db->emptySharers();
	}
	
	public function publish() {

		$title = (string)get_the_title(get_the_id());
		if (($this->db->getSharers() != null) && !(strcasecmp($title, "auto draft" ) == 0 )):
			require_once('urlshortener.php');
			
			$content = get_post_field('post_content', get_the_id());
			$shortURL =  shortenUrl(get_permalink(get_the_ID()));
			$shareText = $this->custom_excerpt($content) . "\n via " . $shortURL['id'];
			$scoopitAPI = new ScoopitAPI($this->db, $this->scoopitOptions);
			$scoopitAPI->publish($title, $content, $shareText);
		endif;
		$this->db->emptySharers();
		
	}
	
	public function custom_excerpt($content) {
		$text = substr($content, 0, 100);
		$ellip ='';
		if (strlen($content) > 100)
			$ellip='…';
		return $text . $ellip;
	}
}
?>